﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DLTN.Entities;
using DLTN.Data;

public partial class DichVu_Controls_ControlDichVuContent : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            HienThiDichVu();
        }
    }

    public string GetTieuDe(object Char)
    {
        string s = Char.ToString();
        return Common.ConvertToUnSign(s);
    }

    public string formatString(string str, int n)
    {
        string kq = "";

        if (str.Length > n)
            kq = str.Substring(0, n + 1) + "...";
        else
            kq = str;

        return kq;
    }


    private void HienThiDichVu()
    {
        string strHTML = "";
        int TotalRows = 0;
        string WhereClause = "1=1";
        TList<LoaiChuyenMuc> TList_LoaiChuyenMuc = DataRepository.LoaiChuyenMucProvider.GetPaged("TenLoaiChuyenMuc like N'%" + Common.DichVu + "%'", "ID", 0, 0, out TotalRows);
        if (TList_LoaiChuyenMuc.Count > 0)
        {
            TList<SubChuyenMuc> TList_SubChuyenMuc = DataRepository.SubChuyenMucProvider.GetPaged("IdLoaiChuyenMuc=" + TList_LoaiChuyenMuc[0].Id, "ID", 0, 0, out TotalRows);
            if (TList_SubChuyenMuc.Count > 0)
            {
                WhereClause += " and IdSubChuyenMuc =" + TList_SubChuyenMuc[0].Id + "or IdSubChuyenMuc=" + TList_SubChuyenMuc[1].Id + "or IdSubChuyenMuc=" + TList_SubChuyenMuc[2].Id + "or IdSubChuyenMuc=" + TList_SubChuyenMuc[3].Id;
                TList<ChuyenMuc> TList_ChuyenMuc = DataRepository.ChuyenMucProvider.GetPaged(WhereClause, "NgayCapNhat DESC", 0, 5, out TotalRows);
                if (TList_ChuyenMuc.Count > 0)
                {
                    lblNgayCapNhat.Text = Common.StandardStringjquery(TList_ChuyenMuc[0].NgayCapNhat.ToString("''dd/MM/yyyy, HH:mm 'GMT+7'"));

                    // Hiển thị 1 dichvu bên trái
                    strHTML += "<div id=\"dichvu-left-text\">";
                    strHTML += "<a href='ChuyenMuc/" + TList_ChuyenMuc[0].Id + "/" + TList_ChuyenMuc[0].IdSubChuyenMuc + "/" + GetTieuDe(TList_ChuyenMuc[0].TieuDe) + ".aspx'>";
                    strHTML += "<img src='" + (TList_ChuyenMuc[0].AnhDaiDien == "no_image.jpg" ? "images/no_image.jpg" : "Upload/ChuyenMuc/" + TList_ChuyenMuc[0].AnhDaiDien) + "' width='374px' height='250px' style='padding-top:2px;'/></a>";
                    strHTML += "<div id='tieude-dichvu'>";
                    strHTML += "<a href='ChuyenMuc/" + TList_ChuyenMuc[0].Id + "/" + TList_ChuyenMuc[0].IdSubChuyenMuc + "/" + GetTieuDe(TList_ChuyenMuc[0].TieuDe) + ".aspx'>";
                    strHTML += TList_ChuyenMuc[0].TieuDe + "</a></div>";
                    strHTML += formatString(TList_ChuyenMuc[0].TomTat.ToString(), 150);
                    strHTML += "</div>";

                    ltrDichVuLeft.Text = strHTML;

                    strHTML = "";

                    // Hiển thị 4 dichvu bên phải
                    for (int i = 1; i < TList_ChuyenMuc.Count; i++)
                    {
                        strHTML += "<div id='dichvu-right'>"; // BEGIN dichvu-RIGHT

                        strHTML += "<div id='dichvu-right-img'>"; // Begin dichvu-right-img
                        strHTML += "<a href='ChuyenMuc/" + TList_ChuyenMuc[i].Id + "/" + TList_ChuyenMuc[i].IdSubChuyenMuc + "/" + GetTieuDe(TList_ChuyenMuc[i].TieuDe) + ".aspx'>";
                        strHTML += "<img src='Upload/ChuyenMuc/" + TList_ChuyenMuc[i].AnhDaiDien + "' width='100px' height='80px'/></a>";
                        strHTML += "</div>"; // End dichvu-right-img

                        strHTML += "<div id='dichvu-right-text'>"; // Begin dichvu-right-text
                        strHTML += "<div id='dichvu-right-text-tieude'>";
                        strHTML += "<a href='ChuyenMuc/" + TList_ChuyenMuc[i].Id + "/" + TList_ChuyenMuc[i].IdSubChuyenMuc + "/" + GetTieuDe(TList_ChuyenMuc[i].TieuDe) + ".aspx'>";
                        if (TList_ChuyenMuc[i].TieuDe.Length > 38)
                            strHTML += formatString(TList_ChuyenMuc[i].TieuDe, 37) + "</a>";
                        else
                            strHTML += TList_ChuyenMuc[i].TieuDe + "</a>";

                        //strHTML += TList_ChuyenMuc[i].TieuDe + "</a>"; 
                        strHTML += "</div>";

                        if (TList_ChuyenMuc[i].TieuDe.Length > 38)
                            strHTML += formatString(TList_ChuyenMuc[i].TomTat.ToString(), 49);
                        else
                            strHTML += formatString(TList_ChuyenMuc[i].TomTat.ToString(), 55);
                        strHTML += "</div>"; // End dichvu-right-text

                        strHTML += "</div>"; // END dichvu-RIGHT
                    }

                    ltrDichVuRight.Text = strHTML;
                }
            }
        }
    }
}